import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Tag } from "../entities/tag.entity";
import { Like, Repository } from "typeorm";
import { MySQLBaseService } from "./mysql-base.service";

@Injectable()
export class TagService extends MySQLBaseService<Tag> {
    constructor(@InjectRepository(Tag) protected repository: Repository<Tag>){
        super(repository)
    }
    async findAll(keyword?: string){
        const where = keyword?[
            {username: Like(`%${keyword}%`)},
            {email: Like(`%${keyword}%`)}
        ]:{}
        return this.repository.find({where})
    }
    async findAllWithPagination(page: number,limit: number,keyword?: string) {
        const where = keyword?[
            {username: Like(`%${keyword}%`)},
            {email: Like(`%${keyword}%`)}
        ]:{}
        const [tags,total] = await this.repository.findAndCount({
            where,
            skip: (page - 1) * limit,
            take: limit
        })
        return {tags,total}
    }
}